var count_time_call_background_task = 0;
var x_default, y_default;
var left_show_defaul, top_show_default;
var position_div;
var position_div_left_origin;
var position_div_top_origin;
var position_div_left_old;
var position_div_top_old;
var delta_left;
var delta_top;
var array_info_tiles;
var array_point;
var insert_point = -1;
var insert_segment_detail_response = -1;
var update_segment_response = -1;
var test_callBack = "";
var zoom_level = 3;

var wd_width;
var wd_height;
var boolean_test = false;

var ajax_stt_responce;

function callback() {
	position_div = $("#dragContainer").position();
	var array_left = new Array(array_info_tiles.length);
	var array_top = new Array(array_info_tiles.length);
	for ( var i = 0; i < array_info_tiles.length; i++) {
		array_left[i] = array_info_tiles[i].left;
		array_top[i] = array_info_tiles[i].top;
	}

	delta_left = -position_div.left - position_div_left_old;
	delta_top = -position_div.top - position_div_top_old;

	position_div_left_old = -position_div.left;
	position_div_top_old = -position_div.top;

	var bound_left = -position_div.left - 256;
	var bound_right = -position_div.left + $(window).width() + 256;
	var bound_top = -position_div.top - 256;
	var bound_bottom = -position_div.top + $(window).height() + 256;

	var max_img_left = check_max(array_left) + 256;
	var min_img_left = check_min(array_left);
	var max_img_top = check_max(array_top) + 256;
	var min_img_top = check_min(array_top);

	var array_min_left = getIndex(min_img_left, array_left);
	var array_max_left = getIndex((max_img_left - 256), array_left);
	var array_min_top = getIndex(min_img_top, array_top);
	var array_max_top = getIndex((max_img_top - 256), array_top);

	var temp_right = bound_right + delta_left;
	var temp_left = bound_left + delta_left;
	var temp_top = bound_top + delta_top;
	var temp_bottom = bound_bottom + delta_top;

	// move left
	if (temp_right >= max_img_left) {
		for ( var i = 0; i < array_min_left.length; i++) {
			var src_old = process_str_src(
					array_info_tiles[array_min_left[i]].x,
					array_info_tiles[array_min_left[i]].y, zoom_level);
			var xy_new = convertXY(array_info_tiles[array_min_left[i]].left,
					array_info_tiles[array_min_left[i]].top,
					array_info_tiles[array_min_left[i]].x,
					array_info_tiles[array_min_left[i]].y, max_img_left,
					array_info_tiles[array_min_left[i]].top);
			// var src_new = process_str_src(xy_new[0], xy_new[1], zoom_level);
			var temp = "img[src='" + src_old + "']";
			$(temp).remove();
			// $(temp).attr('src', src_new).css({"left": max_img_left+"px",
			// "top": array_info_tiles[array_min_left[i]].top+"px"});
			$("#dragContainer").append(
					process_str(max_img_left,
							array_info_tiles[array_min_left[i]].top, xy_new[0],
							xy_new[1], zoom_level));
			array_info_tiles[array_min_left[i]].left = max_img_left;
			array_info_tiles[array_min_left[i]].top = array_info_tiles[array_min_left[i]].top;
			array_info_tiles[array_min_left[i]].x = xy_new[0];
			array_info_tiles[array_min_left[i]].y = xy_new[1];
		}
	}

	// move right
	if (temp_left <= min_img_left) {
		for ( var i = 0; i < array_max_left.length; i++) {
			var src_old = process_str_src(
					array_info_tiles[array_max_left[i]].x,
					array_info_tiles[array_max_left[i]].y, zoom_level);
			var xy_new = convertXY(array_info_tiles[array_max_left[i]].left,
					array_info_tiles[array_max_left[i]].top,
					array_info_tiles[array_max_left[i]].x,
					array_info_tiles[array_max_left[i]].y,
					(min_img_left - 256),
					array_info_tiles[array_max_left[i]].top);
			// var src_new = process_str_src(xy_new[0], xy_new[1], zoom_level);
			var temp = "img[src='" + src_old + "']";
			$(temp).remove();
			// $(temp).attr('src', src_new).css({"left": (min_img_left -
			// 256)+"px", "top": array_info_tiles[array_max_left[i]].top+"px"});
			$("#dragContainer").append(
					process_str((min_img_left - 256),
							array_info_tiles[array_max_left[i]].top, xy_new[0],
							xy_new[1], zoom_level));
			array_info_tiles[array_max_left[i]].left = min_img_left - 256;
			array_info_tiles[array_max_left[i]].top = array_info_tiles[array_max_left[i]].top;
			array_info_tiles[array_max_left[i]].x = xy_new[0];
			array_info_tiles[array_max_left[i]].y = xy_new[1];
		}
	}

	// move bottom
	if (temp_top <= min_img_top) {
		for ( var i = 0; i < array_max_top.length; i++) {
			var src_old = process_str_src(array_info_tiles[array_max_top[i]].x,
					array_info_tiles[array_max_top[i]].y, zoom_level);
			var xy_new = convertXY(array_info_tiles[array_max_top[i]].left,
					array_info_tiles[array_max_top[i]].top,
					array_info_tiles[array_max_top[i]].x,
					array_info_tiles[array_max_top[i]].y,
					array_info_tiles[array_max_top[i]].left,
					(min_img_top - 256));
			// var src_new = process_str_src(xy_new[0], xy_new[1], zoom_level);
			var temp = "img[src='" + src_old + "']";
			$(temp).remove();
			// $(temp).attr('src', src_new).css({"left":
			// array_info_tiles[array_max_top[i]].left+"px", "top": (min_img_top
			// - 256)+"px"});
			$("#dragContainer").append(
					process_str(array_info_tiles[array_max_top[i]].left,
							(min_img_top - 256), xy_new[0], xy_new[1],
							zoom_level));
			array_info_tiles[array_max_top[i]].left = array_info_tiles[array_max_top[i]].left;
			array_info_tiles[array_max_top[i]].top = min_img_top - 256;
			array_info_tiles[array_max_top[i]].x = xy_new[0];
			array_info_tiles[array_max_top[i]].y = xy_new[1];
		}
	}

	// move top
	if (temp_bottom >= max_img_top) {
		// $("span:first").text("temp: " + temp_bottom);
		// $("span:last").text("min_img_top: " + max_img_top);
		for ( var i = 0; i < array_min_top.length; i++) {
			var src_old = process_str_src(array_info_tiles[array_min_top[i]].x,
					array_info_tiles[array_min_top[i]].y, zoom_level);
			var xy_new = convertXY(array_info_tiles[array_min_top[i]].left,
					array_info_tiles[array_min_top[i]].top,
					array_info_tiles[array_min_top[i]].x,
					array_info_tiles[array_min_top[i]].y,
					array_info_tiles[array_min_top[i]].left, max_img_top);
			// var src_new = process_str_src(xy_new[0], xy_new[1], zoom_level);
			var temp = "img[src='" + src_old + "']";
			$(temp).remove();
			// $(temp).attr('src', src_new).css({"left":
			// array_info_tiles[array_min_top[i]].left+"px", "top":
			// max_img_top+"px"});
			$("#dragContainer").append(
					process_str(array_info_tiles[array_min_top[i]].left,
							max_img_top, xy_new[0], xy_new[1], zoom_level));
			array_info_tiles[array_min_top[i]].left = array_info_tiles[array_min_top[i]].left;
			array_info_tiles[array_min_top[i]].top = max_img_top;
			array_info_tiles[array_min_top[i]].x = xy_new[0];
			array_info_tiles[array_min_top[i]].y = xy_new[1];
		}
	}
	setTimeout('callback()', 500);
}

function zoom_map(event, zoom_in) {
	var scale;
	var delta_zoom;
	if (zoom_in) {
		scale = 2;
		delta_zoom = -1;
	} else {
		scale = 1 / 2;
		delta_zoom = 1;
	}
	var tile_x, tile_y;
	var delta_left_convert = $("#dragContainer").position().left
			- position_div_left_origin;
	var delta_top_convert = $("#dragContainer").position().top
			- position_div_top_origin;
	var delta_left_click = (event.pageX - delta_left_convert)
			- left_show_default;
	var delta_top_click = (event.pageY - delta_top_convert) - top_show_default;
	tile_x = x_default + Math.floor(delta_left_click / 256);
	tile_y = y_default + Math.floor(delta_top_click / 256);
	var x_pixel, y_pixel;
	if (delta_left_click < 0) {
		x_pixel = tile_x * 256 + (256 - Math.abs(delta_left_click % 256));
	} else {
		x_pixel = tile_x * 256 + Math.abs(delta_left_click % 256);
	}
	if (delta_top_click < 0) {
		y_pixel = tile_y * 256 + (256 - Math.abs(delta_top_click % 256));
	} else {
		y_pixel = tile_y * 256 + Math.abs(delta_top_click % 256);
	}

	/*
	 * var test_convert_pixel_to_latlng = fromPixelToLatLng(x_pixel, y_pixel,
	 * zoom_level); alert("Latitude: " + test_convert_pixel_to_latlng.lat);
	 * alert("Longitude: " + test_convert_pixel_to_latlng.lng);
	 * 
	 * var test_convert_latlng_to_pixel =
	 * fromLatLngToPixel(test_convert_pixel_to_latlng.lat,
	 * test_convert_pixel_to_latlng.lng, zoom_level); alert("pixel_x: " +
	 * x_pixel); alert("pixel_y: " + y_pixel); alert("pixel_x_convert: " +
	 * test_convert_latlng_to_pixel.x); alert("pixel_y_convert: " +
	 * test_convert_latlng_to_pixel.y);
	 */

	var tile_new_x = Math.floor((x_pixel * scale) / 256);
	var tile_new_y = Math.floor((y_pixel * scale) / 256);
	var delta_new_zoom_x = (x_pixel * scale) - (tile_new_x * 256)
			+ delta_left_convert; // do lech so voi goc toa do x anh moi
	var delta_new_zoom_y = (y_pixel * scale) - (tile_new_y * 256)
			+ delta_top_convert; // do lech so voi goc toa do y anh moi
	$('img').remove();
	zoom_level = zoom_level + delta_zoom;
	x_default = tile_new_x;
	y_default = tile_new_y;
	array_info_tiles = init_map(tile_new_x, tile_new_y,
			(event.pageX - delta_new_zoom_x), (event.pageY - delta_new_zoom_y));
	left_show_default = (event.pageX - delta_new_zoom_x);
	top_show_default = (event.pageY - delta_new_zoom_y);
}

function info_tile(left, top, x, y) {
	this.left = left;
	this.top = top;
	this.x = x;
	this.y = y;
}

function LatLng(lat, lng) {
	this.lat = lat;
	this.lng = lng;
}
function Pixel(x, y) {
	this.x = x;
	this.y = y;
}
function point(id, lat, lng, info) {
	this.id = id;
	this.lat = lat;
	this.lng = lng;
	this.info = info;
}

function convert(x, add) {
	var result;
	result = x + add;
	return result;
}

function compute_number_img(wd_width, wd_height) {
	var result;
	var num_horizon = Math.round(wd_width / 256);
	var num_vertical = Math.round(wd_height / 256);
	result = num_horizon * num_vertical;
	return result;
}

function init_control() {
	$("#name_segment").val("");
	$("#length_segment").val("");
	$("#list_segment_id").val("");
	$("#sequence").val("");
	$("#name_locality").val("");
	$("#latitude").val("");
	$("#longitude").val("");
	$("#list_segment_id").val("");
	$("#list_point").remove();
	$("#zoom_level").val("");
}

function init_map(x_init, y_init, left_show_default, top_show_default) {

	var array_info_tile = new Array(63);
	var count = 0;

	// top
	for ( var i = -1; i > -5; i--) {
		for ( var j = -3; j < 4; j++) {
			$("#dragContainer").append(
					process_str(left_show_default + (256 * i), top_show_default
							+ (256 * j), x_init + i, y_init + j, zoom_level));
			var tile = new info_tile(left_show_default + (256 * i),
					top_show_default + (256 * j), x_init + i, y_init + j);
			array_info_tile[count] = tile;
			count++;
		}
	}

	// center
	for ( var i = -3; i < 4; i++) {
		$("#dragContainer").append(
				process_str(left_show_default, top_show_default + (256 * i),
						x_init, y_init + i, zoom_level));
		var tile = new info_tile(left_show_default, top_show_default
				+ (256 * i), x_init, y_init + i);
		array_info_tile[count] = tile;
		count++;
	}

	// bottom
	for ( var i = 1; i < 5; i++) {
		for ( var j = -3; j < 4; j++) {
			$("#dragContainer").append(
					process_str(left_show_default + (256 * i), top_show_default
							+ (256 * j), x_init + i, y_init + j, zoom_level));
			var tile = new info_tile(left_show_default + (256 * i),
					top_show_default + (256 * j), x_init + i, y_init + j);
			array_info_tile[count] = tile;
			count++;
		}
	}
	return array_info_tile;
}

function process_str(left, top, x, y, z) {
	var default_str = "<img alt='Loading image...' style='position: absolute; top: "
			+ top
			+ "; left: "
			+ left
			+ "' src='./tiles/x="
			+ x
			+ "&y="
			+ y
			+ "&z=" + z + ".png'>";
	return default_str;
}

function process_str_src(x, y, z) {
	var default_str_src = "x=" + x + "&y=" + y + "&z=" + z + ".png";
	return default_str_src;
}

function process_str_style(left, top) {
	var default_str_style = "position: absolute; top: " + top + "; left: "
			+ left;
	return default_str_style;
}

function check_view(top_div, left_div, top_img, left_img, width_div,
		height_div, width_img, height_img) {
	var result;
}

function getXYfromSRC(src) {
	var arr_XY = new Array(2);
	var sub_src = src.substring(src.indexOf("?") + 1, src.length - 2) + "";
	var sub_x = sub_src.substring(sub_src.indexOf("=") + 1, sub_src
			.indexOf("&"));
	var sub_y = sub_src.substring(sub_src.lastIndexOf("=") + 1, sub_src.length);
	arr_XY[0] = sub_x;
	arr_XY[1] = sub_y;
	// alert(sub_src);
	return arr_XY;
}

function check_min(array_ele) {
	var a = array_ele.length;
	var temp = array_ele[0];
	for ( var i = 0; i < a; i++) {
		if (temp > array_ele[i]) {
			temp = array_ele[i];
		}
	}
	return temp;
}

function check_max(array_ele) {
	var a = array_ele.length;
	var temp = array_ele[0];
	for ( var i = 0; i < a; i++) {
		if (temp < array_ele[i]) {
			temp = array_ele[i];
		}
	}
	return temp;
}

function getIndex(value, array) {
	var a = array.length;
	var result = new Array(7);
	var count = 0;
	for ( var i = 0; i < a; i++) {
		if (value == array[i]) {
			result[count] = i;
			count++;
		}
	}

	return result;
}
function convertXY(left_old, top_old, x_old, y_old, left_new, top_new) {
	var XY_new = new Array(2);
	XY_new[0] = (left_new - left_old) / 256 + x_old;
	XY_new[1] = (top_new - top_old) / 256 + y_old;
	return XY_new;
}

function fromLatLngToPixel(lat, lng, z) {
	// longitude -> x
	// latitude -> y
	var offset = 16777216;
	var radius = offset / Math.PI;

	var x = (offset + radius * (lng * Math.PI / 180)) >> z;
	var y = (offset - radius
			* Math.log((1 + Math.sin(lat * Math.PI / 180))
					/ (1 - Math.sin(lat * Math.PI / 180))) / 2) >> z;

	var pixel = new Pixel(x, y);
	return pixel;
}

function fromPixelToLatLng(x, y, z) {
	var offset = 16777216;
	var radius = offset / Math.PI;

	var temp1 = ((y << z) - offset) / radius;
	var temp2 = 2 * Math.atan(Math.exp(temp1));
	var lat = ((Math.PI / 2 - temp2) * 180) / Math.PI;

	var lng = ((((x << z) - offset) / radius) * 180) / Math.PI;

	var latlng = new LatLng(lat, lng);
	return latlng;
}

function DecToDeg(d) {
	return d * Math.PI / 180.0;
}

function round_f(num, ext) {
	var tmp = Math.pow(10, ext);
	return (Math.round(num * tmp) / tmp);
}